package net.oschina.esb.webmvc.auto;

import net.oschina.esb.core.AbstractSpringBootRegister;
import net.oschina.esb.util.ClassScanner;
import net.oschina.esb.util.GenericUtil;
import net.oschina.esb.webmvc.auto.parameter.PageParameter;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Set;

/**
 * 注册 PageParameter
 *
 * @author Arvin
 * @time 2018/5/5 8:39
 */
@Configuration
public class AutoPageParameterSpringRegister extends AbstractSpringBootRegister {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, Environment environment, ApplicationContext applicationContext) {

        Set<Class<?>> classes = ClassScanner.scan(PageParameter.class, PageParameter.class.getPackage().getName());

        if (classes != null && !classes.isEmpty()) {
            for (Class<?> clazz : classes) {
                logger.info("正在自动注册 PageParameter: {}", clazz.getName());
                GenericBeanDefinition parameter = new GenericBeanDefinition();
                parameter.setBeanClass(clazz);
                registry.registerBeanDefinition(GenericUtil.firstLetterToLowerCase(clazz.getSimpleName()), parameter);
            }
        }

    }
}
